<?php

namespace SmartWiki\Models;
use DB;
use Cache;
use Carbon\Carbon;
use SmartWiki\Exceptions\DataNullException;
use SmartWiki\Exceptions\FormatException;
use SmartWiki\Exceptions\ResultException;

/**
 * SmartWiki\Models\Axure
 *
 * @mixin \Eloquent
 * @property string $axure_id  ID
 * @property string $axure_name 原型名称
 * @property integer $project_id 所属项目
 * @property string $axure_url 原型地址
 * @property boolean $remote 是否远程地址
 * @property string $create_time
 * @property integer $create_at
 * @property string $modify_time
 * @property integer $modify_at
 * @property string $version 当前时间戳
 * @method static \Illuminate\Database\Query\Builder|Axure whereAxureId($value)
 * @method static \Illuminate\Database\Query\Builder|Axure whereAxureName($value)
 * @method static \Illuminate\Database\Query\Builder|Axure whereProjectId($value)
 * @method static \Illuminate\Database\Query\Builder|Axure whereAxureUrl($value)
 * @method static \Illuminate\Database\Query\Builder|Axure whereCreateTime($value)
 * @method static \Illuminate\Database\Query\Builder|Axure whereCreateAt($value)
 * @method static \Illuminate\Database\Query\Builder|Axure whereModifyTime($value)
 * @method static \Illuminate\Database\Query\Builder|Axure whereModifyAt($value)
 * @method static \Illuminate\Database\Query\Builder|Axure whereVersion($value)
 */
class Axure extends ModelBase
{
    protected $table = 'axure';
    protected $primaryKey = 'axure_id';
    protected $keyType = "char";
    protected $dateFormat = 'Y-m-d H:i:s';
    protected $guarded = ['axure_id'];

    public $timestamps = false;

    /**
     * 从缓存中获取指定的文档
     * @param $axure_id
     * @param bool $update 是否强制更新缓存
     * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model|null|Axure|Axure[]
     */
    public static function getAxureFromCache($axure_id,$update = false)
    {
        $key = 'axure.axure_id.'.$axure_id;
        $axure = $update or Cache::get($key);

        if(empty($axure) or $update){
            $axure = Axure::find($axure_id);
            $expiresAt = Carbon::now()->addHour(12);

            Cache::put($key,$axure, $expiresAt);
        }
        return $axure;
    }


    /**
     * 删除
     * @param $axure_id
     * @return bool
     */
    public static function deleteAxure($axure_id)
    {
        $axure = Axure::find($axure_id);
        $axure->delete();
        return true;
    }


    /**
     * 获取原型列表
     * @param int $project_id
     * @return array
     */
    public static function getAxures($project_id)
    {
        if(empty($project_id)){
            return [];
        }
        $jsonArray = Axure::where('project_id','=',$project_id)
            ->select(['axure_id', 'project_id', 'axure_name', 'axure_url', 'remote'])
            ->get();
       
        return $jsonArray;
    }

    /**
     * 添加或更新原型
     * @return bool
     * @throws \Exception
     */
    public function addOrUpdate()
    {
        if(empty($this->axure_name) || mb_strlen($this->axure_name) < 2 || mb_strlen($this->axure_name) > 50){
            throw new FormatException('原型名称必须在2-50字之间',70201);
        }

        if(in_array($this->remote,['0','1']) === false){
            throw new FormatException('原型模式错误',70202);
        }

        if($this->remote == 0 and (strlen($this->axure_url) == 0)){
            throw new FormatException('请上传原型文件',70203);
        }
        else if($this->remote == 1 and (strlen($this->axure_url) == 0)){
            throw new FormatException('请设置原型地址',70204);
        }

        DB::beginTransaction();
        try{

            if($this->create_time == null){

            }
            $this->save();

            DB::commit();
            return true;
        }catch (\Exception $ex){
            DB::rollBack();
            throw new ResultException($ex->getMessage(),500);
        }
    }

}
